package com.example.timing_wheel.tools;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Data
@Slf4j
public class TimerTaskEntry implements Comparable<TimerTaskEntry> {

    volatile TimerTaskList timerTaskList;
    TimerTaskEntry next;
    TimerTaskEntry prev;
    private TimerTask timerTask;
    private long expireMs;

    public TimerTaskEntry(TimerTask timerTask, long expireMs) {
        this.timerTask = timerTask;
        this.expireMs = expireMs;
        this.next = null;
        this.prev = null;
    }

    // 删除 TimerTaskList 中对应的 TimerTaskEntry 节点
    public void remove() {
        TimerTaskList currentList = timerTaskList;
        while (currentList != null) {
            currentList.remove(this);
            currentList = timerTaskList;
        }
    }

    @Override
    public int compareTo(TimerTaskEntry o) {
        return (int) (this.expireMs - o.expireMs);
    }
}
